package logic

import (
	"context"
	"errors"
	"video-system/model/mysql"

	"video-system/userrpc/internal/svc"
	"video-system/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectAddLogic {
	return &CollectAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 添加收藏
func (l *CollectAddLogic) CollectAdd(in *userrpc.CollectAddRequest) (*userrpc.CollectAddResponse, error) {
	// todo: add your logic here and delete this line

	var collcet mysql.CollectionInfo

	if err := collcet.FindCollectId(mysql.DB, in.UserId, in.VideoId); err != nil {
		return nil, err
	}

	if collcet.ID != 0 {
		return nil, errors.New("已收藏")
	}

	collection := &mysql.CollectionInfo{
		UserId:        in.UserId,
		VideoId:       in.VideoId,
		CollectStatus: in.CollectStatus,
	}

	if err := collection.CollectAdd(mysql.DB); err != nil {
		return nil, err
	}

	return &userrpc.CollectAddResponse{
		CollectId: int64(collection.ID),
	}, nil
}
